package thread;

public class ThreadDemo22 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(() ->{
            synchronized (A) {
                try {
                    //sleep一下给t2时间也能拿到B
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //尝试获取B,并没有释放A
                synchronized (B){
                    System.out.println("t1 拿到了俩把锁");
                }
            }
        });

        Thread t2 = new Thread(() ->{
            synchronized (B) {//改为A
                try {
                    //sleep一下给t1时间也能拿到A
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //尝试获取A,并没有释放B
                synchronized (A){//改为B就可以解决
                    System.out.println("t2 拿到了俩把锁");
                }
            }

        });
        t1.start();
        t2.start();


    }
}
